﻿using AiMoWindows.CodeScripts;
using AiMoWindows.Localization;
using System.Windows;

namespace AiMoWindows.Windows.Properties
{
    /// <summary>
    /// PropertiesWindow.xaml 的交互逻辑
    /// </summary>
    partial class PropertiesWindow : Window
    {
        readonly string _initAuthor;
        readonly string _initDescription;

        string _newAuthor;
        string _newDescription;

        public PropertiesWindow(ScriptSource script, Microsoft.CodeAnalysis.MetadataReference[] metadataReferences)
        {
            DataContext = new PropertiesViewModel(script.Attributes.ScreenShot);
            InitializeComponent();
            Title = script.Attributes.CodeName;
            string info =
                $"{(Local.RunningInChinese ? "代码名字" : "Code Name")}:  {script.Attributes.CodeName}" + Environment.NewLine +
                $"{(Local.RunningInChinese ? "代码来源" : "Source")}:  {script.Attributes.Source}" + Environment.NewLine +
                $"{(Local.RunningInChinese ? "代码用户" : "User Name")}:  {script.Attributes.UserName}" + Environment.NewLine +
                $"{(Local.RunningInChinese ? "代码模式" : "Scripts Mode")}:  {script.ScriptsMode}" + Environment.NewLine +
                $"{(Local.RunningInChinese ? "Ai问题" : "Question")}:  {script.Attributes.Question}" + Environment.NewLine +
                $"{(Local.RunningInChinese ? "Rhino版本" : "Rhino Version")}:  {script.Attributes.RhinoVersion}" + Environment.NewLine + Environment.NewLine;

            info += (Local.RunningInChinese ? "载入的Dll" : "Reference Loaded:") + Environment.NewLine;
            foreach (var reference in metadataReferences)
            {
                info += $"    {reference.Display}{Environment.NewLine}";
            }
            InfoTextBox.Text = info;

            _initAuthor = script.Attributes.Author;
            _newAuthor = _initAuthor;
            AuthorTextBox.Text = _initAuthor;

            _initDescription = script.Attributes.Description;
            _newDescription = _initDescription;
            DescriptionTextBox.Text = _initDescription;
        }

        public bool GetAuthorChanged(out string author)
        {
            author = _newAuthor;
            return _newAuthor != _initAuthor;
        }
        public bool GetDescriptionChanged(out string description)
        {
            description = _newDescription;
            return _newDescription != _initDescription;
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            _newAuthor = AuthorTextBox.Text.Trim();
            _newDescription = DescriptionTextBox.Text.Trim();
            DialogResult = true;
            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}